//
//  LogicSB.m
//  tcpoctest
//
//  Created by gump on 11/6/12.
//  Copyright (c) 2012 gump. All rights reserved.
//

#import "LogicSB.h"
#import "NSString+URLEncoding.h"

#import <pthread.h>

@implementation LogicSB

-(id)init
{
    self = [super init];
    
    if(self)
    {
        mdata = 0;
        mlength = 0;
        mreadpoint = 0;
        self.mrestLength = 0;
        mwritepoint = 0;
    }
    return self;
}

-(void)initSB:(char *)data withLength:(int)len
{
    mdata = data;
    mlength = len;
    mreadpoint = 0;
    self.mrestLength = len;
    mwritepoint = 0;
}

-(void)cleanSB
{
    mdata = 0;
    mlength = 0;
    mreadpoint = 0;
    self.mrestLength = 0;
    mwritepoint = 0;
}

-(char)ReadChar
{
    char rc;
    memcpy(&rc, mdata + mreadpoint, CHAR_PL);
    
    mreadpoint += CHAR_PL;
    self.mrestLength -= CHAR_PL;
    
    return rc;
}

-(NSString *)ReadRestString
{
    NSString *str = nil;
    
    int restlen = mlength - mreadpoint;
    if(restlen > 0)
    {
        char    *data = (char *)malloc(restlen + 1);
        assert(data != nil);
        memcpy(data, mdata + mreadpoint, restlen);
        data[restlen] = '\0';
        memcpy(data, data, strlen(data));
        str = [NSString stringWithCString:data encoding:NSUTF8StringEncoding];
        self.mrestLength = 0;
        free(data);
    }
    
    return str;
}

-(NSData *)ReadDataWithLength:(int)ilength
{
    self.mrestLength -= ilength;
    
    char *data = 0;
    NSData *nsdata = nil;
    if(self.mrestLength >= 0)
    {
        data = (char *)malloc(ilength);
        assert(data != nil);
        memcpy(data, mdata + mreadpoint, ilength);
        nsdata = [NSData dataWithBytes:data length:ilength];
        free(data);
        
        mreadpoint += ilength;
    }
    
    return nsdata;
}

-(int)ReadIntWithTT:(int)itt
{
    int i = 0;
    memcpy(&i, mdata + mreadpoint, INT_PL);
    
    mreadpoint += INT_PL;
    self.mrestLength -= INT_PL;
    
    switch (itt) {
        case NTT_BIG2SMALL:
        {
            i = ntohl(i);
        }
            break;
        case NTT_SMALL2BIG:
        {
            i = htonl(i);
        }
            break;
        default:
            break;
    }
    
    return i;
}

-(NSString *)ReadUnencodeString
{
    NSString *str = @"";
    
    int restlen = mlength - mreadpoint;
    if(restlen > 0)
    {
        char length = [self ReadChar];
        char    *data = (char *)malloc(length + 1);
        assert(data != nil);
        memcpy(data, mdata + mreadpoint, length);
        data[length] = '\0';
        memcpy(data, data, strlen(data));
        str = [NSString stringWithCString:data encoding:NSUTF8StringEncoding];
        free(data);
        
        mreadpoint += length;
        self.mrestLength -= length;
    }
    
    return str;
}

-(NSString *)ReadStringWithLength:(int)il
{
    NSString *str = @"";
    
    int restlen = mlength - mreadpoint;
    if(restlen > 0)
    {
        char    *data = (char *)malloc(il + 1);
        assert(data != nil);
        memcpy(data, mdata + mreadpoint, il);
        data[il] = '\0';
        memcpy(data, data, strlen(data));
        str = [NSString stringWithCString:data encoding:NSUTF8StringEncoding];
        free(data);
        
        mreadpoint += il;
        self.mrestLength -= il;
    }
    
    return str;
}

-(void)WriteString:(NSString *)str withLength:(int)len
{
    
    self.mrestLength -= len;
    if (self.mrestLength < 0)
    {
        return;
    }
    
    memcpy(mdata + mwritepoint,[str UTF8String],len);
    mwritepoint += len;
}

-(void)WriteChar:(char)value
{
    
    self.mrestLength -= CHAR_PL;
    if (self.mrestLength < 0)
    {
        return;
    }
    
    memcpy(mdata + mwritepoint, &value, CHAR_PL);
    mwritepoint += CHAR_PL;
}

-(void)WriteInt:(int)value withNTT:(int)intt
{
    
    self.mrestLength -= INT_PL;
    if(self.mrestLength < 0)
    {
        return;
    }
    
    switch (intt) {
        case NTT_BIG2SMALL:
        {
            value = ntohl(value);//网络转到本地
        }
            break;
            case NTT_SMALL2BIG:
        {
            value = htonl(value);//本地转到网络
        }
            break;
        default:
            break;
    }
    
    
    memcpy(mdata + mwritepoint, &value,INT_PL);
    mwritepoint += INT_PL;
}

-(void)WriteData:(char *)data withLength:(int)len
{
    
    self.mrestLength -= len;
    if(self.mrestLength < 0)
    {
        return;
    }
    
    memcpy(mdata + mwritepoint, data, len);
    mwritepoint += len;
}

-(void)WriteFixedLengthString:(NSString *)str withLength:(int)len
{
    
    self.mrestLength -= len;
    if(self.mrestLength < 0)
    {
        return;
    }
    
    const char* strchar = [str UTF8String];
    memcpy(mdata + mwritepoint, [str UTF8String], strlen(strchar));
    mwritepoint += len;
}

@end
